namespace Wayloop.Fusion.Core.Objects
{
    public abstract class FusionObject<IdT>
    {
        protected IdT id;

        public virtual IdT Id
        {
            get { return id; }
            protected set { id = value; }
        }

        public virtual bool IsTransient
        {
            get { return id.Equals(default(IdT)) || Id.Equals(null); }
        }

        public abstract override int GetHashCode();


        public override bool Equals(object obj)
        {
            FusionObject<IdT> other = obj as FusionObject<IdT>;

            if (other != null && HasSameId(other) && GetHashCode().Equals(other.GetHashCode()))
                return true;

            return false;
        }


        protected int GetHashCode(params object[] values)
        {
            int hashCode = 0;

            foreach (object o in values)
                hashCode ^= o.GetHashCode();

            return hashCode;
        }


        private bool HasSameId(FusionObject<IdT> other)
        {
            if (!IsTransient && !other.IsTransient && Id.Equals(other.Id))
                return true;

            return false;
        }
    }
}